﻿using System.Windows;
using DSPL.Services;
using DSPL.Contracts;
using DSPL.Common;
using System.Data;
using DSPL.Services.Services;
using DSPL.Contracts.Common;
using DSPL.Contracts.ViewModel;
using DSPL.Contracts.Command;
using DSPL.Finac.Common;
using System;
using System.Windows.Input;

namespace DSPL.Finac.ViewModel
{
    public class PurchaseOrderViewModel : BaseViewModel
    {
        private bool isRefNoFocused;
        private bool isCurrencyCodeFocused;
        private bool isSupplierCodeFocused;
        private bool isCostCentreFocused;
        private bool isTxnDateFocused;
        private DataRow drItemRow;       
        private ICommand cellEditEnding;

        #region Define properties

        public bool IsCurrencyCodeFocused
        {
            get { return isCurrencyCodeFocused; }
            set
            {
                isCurrencyCodeFocused = value;
                this.OnPropertyChanged("IsCurrencyCodeFocused");
            }
        }

        public bool IsSupplierCodeFocused
        {
            get { return isSupplierCodeFocused; }
            set
            {
                isSupplierCodeFocused = value;
                this.OnPropertyChanged("IsSupplierCodeFocused");

            }
        }

        public bool IsCostCentreFocused
        {
            get { return isCostCentreFocused; }
            set
            {
                isCostCentreFocused = value;
                this.OnPropertyChanged("IsCostCentreFocused");

            }
        }

        public bool IsRefNoFocused
        {
            get { return isRefNoFocused; }
            set
            {
                isRefNoFocused = value;
                this.OnPropertyChanged("IsRefNoFocused");

            }
        }

        public bool IsTxnDateFocused
        {
            get { return isTxnDateFocused; }
            set
            {
                isTxnDateFocused = value;
                this.OnPropertyChanged("IsTxnDateFocused");

            }
        }

        public DataRow ItemSelectedRow
        {
            get
            {
                return drItemRow;
            }
            set
            {
                drItemRow = value;
                OnPropertyChanged("ItemSelectedRow");
            }
        }

      

        public ICommand CellEditEnding
        {
            get
            {
                if (this.cellEditEnding == null)
                    this.cellEditEnding = new RelayCommand(CellEditEndingExecuted);

                return this.cellEditEnding;
            }
        }        

        #endregion


        #region Constructor

        public PurchaseOrderViewModel()
            : base()
        {
            MessageTitle = "Purchase Order ";
        }

        #endregion

        /// <summary>
        /// Validate mandatory data before save
        /// </summary>
        /// <param name="obj"></param>
        protected override void PreDataValidateExecuted(object obj)
        {
            IsRefNoFocused = false;
            IsCurrencyCodeFocused = false;
            IsSupplierCodeFocused = false;
            IsCostCentreFocused = false;
            IsTxnDateFocused = false;

            var messageService = new MessageService();
            if (String.IsNullOrEmpty(CurrentRow["RefNo"].ToString().Trim()))
            {
                messageService.ShowMessage(MessageText.ReferenceNoNotEmpty, MessageTitle, MessageType.InfoOK);
                IsRefNoFocused  = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["CurPk"]==DBNull.Value || Convert.ToInt32(CurrentRow["CurPk"]) == 0)
            {
                messageService.ShowMessage(MessageText.CurrencyCodeNotEmpty, MessageTitle, MessageType.InfoOK);
                IsCurrencyCodeFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["SupplierPk"] == DBNull.Value || Convert.ToInt32(CurrentRow["SupplierPk"]) == 0)
            {
                messageService.ShowMessage(MessageText.SupplierNotEmpty, MessageTitle, MessageType.InfoOK);
                IsSupplierCodeFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["CostPk"] == DBNull.Value || Convert.ToInt32(CurrentRow["CostPk"]) == 0)
            {
                messageService.ShowMessage(MessageText.CostCodeNotEmpty, MessageTitle, MessageType.InfoOK);
                IsCostCentreFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["TxnDate"] == DBNull.Value)
            {
                messageService.ShowMessage(MessageText.TransactionDateNotEmpty, MessageTitle, MessageType.InfoOK);
                IsTxnDateFocused = true;
                this.IsValid = false;
                return;
            }

            this.IsValid = true;
        }

        protected override  void PostResetDefaultExecuted(object obj)
        {
            DtChildTransaction.Columns.Add("dtcItemCode");
            DtChildTransaction.Columns.Add("dtcItemDes");
            DtChildTransaction.Columns.Add("dtcTaxCode");
            DataRow d1=DtChildTransaction.NewRow();
            DtChildTransaction.Rows.Add(d1);
        }

        private void CellEditEndingExecuted(object obj)
        {
            //OnPropertyChanged("DtChildTransaction");
        }
    }
}


